package com.example.check_in.controller;


import com.example.check_in.service.CheckInService;
import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.check_in.CheckIn;
import com.example.model.check_in.OrderDo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/checkIn")
public class CheckInController {

    @Autowired
    private CheckInService checkInService;

    @HasPerm("accommodation:stay:startCheckIn")
    @PostMapping("/start")
    public Result startCheckIn() {
        return Result.success(checkInService.start());
    }

    @HasPerm("accommodation:stay:enterInfo")
    @PostMapping("/enterInfo")
    public Result enterInfo(@Valid @RequestBody OrderDo order) {
        System.out.println(order.toString());
        return Result.success(checkInService.enterInfo(order));
    }

    @PostMapping("/loadCheckIn")
    public Result loadCheckIn() {
        return Result.success(checkInService.loadCheckIn());
    }

    @HasPerm("accommodation:stay:makePayment")
    @PostMapping(value = "/makePayment")
    public Result makePayment(@RequestParam("paymentType")String paymentType,@RequestParam("pay") BigDecimal pay) {
        return Result.success(checkInService.makePayment(paymentType,pay));
    }

    @PostMapping("/delayPay")
    public Result delayPay(@RequestParam("checkInId") Long checkInId,
                           @RequestParam("paymentType") String paymentType,
                           @RequestParam("pay") BigDecimal pay) {
        return Result.success(checkInService.delayPay(checkInId, paymentType, pay));
    }


    @HasPerm("accommodation:stay:clear")
    @PostMapping("/clear")
    public Result clear() {
        //TODO 后面优化
        checkInService.clear();
        return Result.success("ok");
    }

    @GetMapping("/list")
    @HasPerm("accommodation:stay:list")
    public Result list() {
        List<CheckIn> list = checkInService.list(null);
        return Result.success(list);
    }

    @HasPerm("accommodation:stay:list")
    @GetMapping("/page")
    public Result list(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize, CheckIn checkIn) {
        Page<CheckIn> page = PageHelper.startPage(pageNum, pageSize);
        List<CheckIn> list = checkInService.list(checkIn);
        PageInfo<CheckIn> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }


    @HasPerm("accommodation:stay:delete")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(checkInService.delete(id) == 1 ? "ok" : "fail");
    }


    @HasPerm("accommodation:stay:checkOut")
    @PostMapping("/checkOut")
    public Result checkOut(@RequestParam("checkInId")Long checkInId) {
        checkInService.checkOut(checkInId);
        return Result.success("success");
    }

    @HasPerm("accommodation:stay:checkOutList")
    @GetMapping("/checkOutList")
    public Result checkOutList() {
        return Result.success(checkInService.checkOutList());
    }

    @GetMapping("/isEnable")
    public Result isEnable(@RequestParam("roomId")Long roomId, @RequestParam("checkInDate")String checkInDate) {
        return Result.success(checkInService.isEnable(roomId, checkInDate));
    }



    @HasPerm("management:achievement:list")
    @GetMapping("/listData")
    public Result listData(@RequestParam("type") String type){
        return Result.success(checkInService.listData(type));
    }

    @HasPerm("management:achievement:list")
    @GetMapping("/listDataByRoomType")
    public  Result listDataByRoomType(@RequestParam("type") String type,@RequestParam("roomTypeName") String  roomTypeName){
        return Result.success(checkInService.listDataByRoomType(type,roomTypeName));
    }
}
